package com.example.Dao;

import com.example.JDBCConnection;
import com.example.pojo.User;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * ClassName: UserDAO
 * Package: com.example.Dao
 * Description:
 *
 * @Author: liu
 * @Create: 2025/5/28 10:45
 * @Version:1.0
 */
public class UserDAO {
    // 用户登录（返回用户对象，失败返回null）
    public User login(String id, String password) {
        String sql = "SELECT * FROM user WHERE id = ? AND password = ?";
        try (Connection conn = JDBCConnection.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {
            pstmt.setString(1, id);
            pstmt.setString(2, password);
            ResultSet rs = pstmt.executeQuery();
            if (rs.next()) {
                return new User(
                        rs.getString("id"),
                        rs.getString("name"),
                        rs.getString("password"),
                        rs.getString("role")
                );
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }
}
